package springboot.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * TTL队列的配置类
 */
@Configuration
public class TTLQueueConfig {
    
    // 普通交换机名称
    public static final String X_CHANGE = "X";
    // 死信交换机名称
    public static final String Y_DEAD_LETTER_EXCHANGE = "Y";
    
    // 普通队列名称
    public static final String QUEUE_A = "QA";
    public static final String QUEUE_B = "QB";
    public static final String QUEUE_C = "QC";
    // 死信队列名称
    public static final String QUEUE_D = "QD";
    
    // 普通队列路由key
    public static final String XA = "XA";
    public static final String XB = "XB";
    public static final String XC = "XC";
    // 死信队列路由key
    public static final String YD = "YD";

    /**
     * 普通交换机
     * @return 普通交换机
     */
    @Bean(value = "xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(X_CHANGE);
    }

    /**
     * 死信交换机
     * @return 死信交换机
     */
    @Bean(value = "yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(Y_DEAD_LETTER_EXCHANGE);
    }

    /**
     * 普通队列QA,TTL为10s
     * @return 普通队列QA
     */
    @Bean(value = "queueA")
    public Queue queueA(){
        
        Map<String,Object> arguments = new HashMap<>(3);
        // 设置过期时间10s
        arguments.put("x-message-ttl", 10000);
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        // 设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key", YD);
        return QueueBuilder
                .durable(QUEUE_A)
                .withArguments(arguments)
                .build();
    }

    /**
     * 普通队列QB,TTL为40s
     * @return 普通队列QB
     */
    @Bean(value = "queueB")
    public Queue queueB(){

        Map<String,Object> arguments = new HashMap<>(3);
        // 设置过期时间40s
        arguments.put("x-message-ttl", 40000);
        // 设置死信交换机
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        // 设置死信RoutingKey
        arguments.put("x-dead-letter-routing-key", YD);
        return QueueBuilder
                .durable(QUEUE_B)
                .withArguments(arguments)
                .build();
    }
    /**
     * 普通队列QC,不设置过期时间
     * @return 普通队列QC
     */
    @Bean(value = "queueC")
    public Queue queueC(){
        Map<String,Object> arguments = new HashMap<>(3);
        arguments.put("x-dead-letter-exchange", Y_DEAD_LETTER_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", YD);
        return QueueBuilder.durable(QUEUE_C)
                .withArguments(arguments)
                .build();
    }
    /**
     * 死信队列QD
     * @return 死信队列QD
     */
    @Bean(value = "queueD")
    public Queue queueD(){
        return QueueBuilder.durable(QUEUE_D).build();
    }

    /**
     * 普通队列QA绑定X交换机
     * @param queueA 队列
     * @param directExchange 交换机
     * @return 绑定
     */
    @Bean
    public Binding queueABindingX(@Qualifier(value = "queueA") Queue queueA,
                                  @Qualifier(value = "xExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queueA).to(directExchange).with(XA);
    }

    /**
     * 普通队列QB绑定普通交换机X
     * @param queueB 队列
     * @param directExchange 交换机
     * @return 绑定
     */
    @Bean
    public Binding queueBBindingX(@Qualifier(value = "queueB") Queue queueB,
                                  @Qualifier(value = "xExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queueB).to(directExchange).with(XB);
    }

    /**
     * 普通队列QC绑定普通交换机X
     * @param queueC 队列
     * @param directExchange 交换机
     * @return 绑定
     */
    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queueC).to(directExchange).with(XC);
    }
    /**
     * 死信队列QD绑定死信交换机Y
     * @param queueD 队列
     * @param directExchange 交换机
     * @return 绑定
     */
    @Bean
    public Binding queueDBindingY(@Qualifier(value = "queueD") Queue queueD,
                                  @Qualifier(value = "yExchange") DirectExchange directExchange){
        return BindingBuilder.bind(queueD).to(directExchange).with(YD);
    }
}
